package com.violet.detective.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.violet.detective.entity.Workorder;
import com.violet.detective.search.WorkorderSearchParameter;
import com.violet.detective.service.EquipmentService;
import com.violet.detective.service.WorkorderService;
import com.violet.detective.mapper.WorkorderMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 21145
* @description 针对表【WorkOrder】的数据库操作Service实现
* @createDate 2023-09-04 21:42:54
*/
@Service
public class WorkorderServiceImpl extends ServiceImpl<WorkorderMapper, Workorder>
    implements WorkorderService{

    @Resource
    WorkorderMapper workorderMapper;

    @Resource
    private EquipmentService equipmentService;

    public boolean addWorkOrder(Workorder workorder) {
        int result = workorderMapper.insert(workorder);
        return result > 0;
    }

    public boolean editWorkOrder(Workorder updatedWorkorder) {
        int result = workorderMapper.updateById(updatedWorkorder);
        return result > 0;
    }

    public boolean deleteWorkOrder(Integer id) {
        int result = workorderMapper.deleteById(id);
        return result > 0;
    }

    public Workorder getWorkOrderById(Integer id) {
        return workorderMapper.selectById(id);
    }

    public List<Workorder> searchWorkOrders(WorkorderSearchParameter searchParameter) {
        QueryWrapper<Workorder> query = new QueryWrapper<>();
        if (searchParameter.getEquipmentId() != null)  // 设备编码
            query.eq("equipmentId", searchParameter.getEquipmentId());
        if (searchParameter.getState() != null)  // 状态
            query.eq("state", searchParameter.getState());
        if (searchParameter.getIsClosed() != null && searchParameter.getIsClosed())  // 是否关闭
            query.eq("state", "closed");
        if (searchParameter.getFixType() != null)  // 维修类型
            query.eq("fixType", searchParameter.getFixType());
        if (searchParameter.getContent() != null)  //内容
            query.like("content", searchParameter.getContent());
        if (searchParameter.getUpdateTime() != null)
            query.ge("updateTime", searchParameter.getUpdateTime());
        if (searchParameter.getEquipmentName() != null)
            query.like("equipmentName", searchParameter.getEquipmentName());
        if (searchParameter.getEquipmentMajor() != null)
            query.like("equipmentMajor", searchParameter.getEquipmentMajor());

        return workorderMapper.selectList(query);
    }

    public Page<Workorder> searchWorkOrders(WorkorderSearchParameter searchParameter, int pageNumber, int pageSize) {
        Page<Workorder> page = new Page<>(pageNumber, pageSize);
        QueryWrapper<Workorder> query = new QueryWrapper<>();
        if (searchParameter.getEquipmentId() != null) // 设备编码
            query.eq("equipmentId", searchParameter.getEquipmentId());
        if (searchParameter.getState() != null) // 状态
            query.eq("state", searchParameter.getState());
        if (searchParameter.getIsClosed() != null && searchParameter.getIsClosed()) // 是否关闭
            query.eq("state", "closed");
        if (searchParameter.getFixType() != null) // 维修类型
            query.eq("fixType", searchParameter.getFixType());
        if (searchParameter.getContent() != null) //内容
            query.like("content", searchParameter.getContent());
        if (searchParameter.getUpdateTime() != null)
            query.ge("updateTime", searchParameter.getUpdateTime());
        if (searchParameter.getEquipmentName() != null)
            query.like("equipmentName", searchParameter.getEquipmentName());
        if (searchParameter.getEquipmentMajor() != null)
            query.like("equipmentMajor", searchParameter.getEquipmentMajor());

        return workorderMapper.selectPage(page, query);
    }


    public List<Workorder> findAllWorkOrders() {
        return workorderMapper.selectList(null);
    }

    public Page<Workorder> findAllWorkOrders(int pageNum, int pageSize) {
        Page<Workorder> page = new Page<>(pageNum, pageSize);
        return workorderMapper.selectPage(page,null);
    }

}




